//
//  YSMeBianJiViewController.m
//  YouSheng
//
//  Created by Mac on 16/8/25.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "YSMeBianJiViewController.h"
#import "MeXinxCell.h"
#import "BianJiNameViewController.h"
#import "YSQRCodeViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <Masonry/Masonry.h>

#define YSBJColorHui [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1]
//图片按钮上线
#define BTNCOUNT 7
//每行最大个数
#define COLCOUNT 4
//间距
#define MARGIN 10

static NSString * const bjCellID = @"bjCellID";

@interface YSMeBianJiViewController ()<UITableViewDelegate,UITableViewDataSource>

//{
//    CGFloat _btnWandH;
//}

@property(nonatomic,weak) UIView *iconScrollView;

@property(nonatomic,weak) UITableView *bottomTableView;

////按钮的宽高
//@property(nonatomic,assign) CGFloat *btnWandH;

//存放头像按钮的数组
@property(nonatomic,strong) NSMutableArray *btnArr;

//存tableview信息的数组
@property(nonatomic,strong) NSArray *xinxArr;
//存tableview右边信息的数组
@property(nonatomic,strong) NSArray *youArr;

@property(nonatomic,weak) UIButton *iconBtn;

@end

@implementation YSMeBianJiViewController

#pragma mark - 懒加载
- (NSArray *)xinxArr {
    if (!_xinxArr) {
        _xinxArr = @[@"名字",@"二维码",@"性别",@"年龄",@"个性签名",@"所在地",@"家乡",@"行业"];
    }
    return _xinxArr;
}

- (NSArray *)youArr {
    if (!_youArr) {
        _youArr = @[@"紫薇",@"",@"女",@"18",@"你好你好",@"紫禁城",@"北京",@"演员"];
    }
    
    return _youArr;
}

- (NSMutableArray *)btnArr {
    if (!_btnArr) {
        _btnArr = [NSMutableArray array];
    }
    
    return _btnArr;
}

- (UIButton *)iconBtn {
    if (!_iconBtn) {
        _iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_iconBtn setImage:[UIImage imageNamed:@"me_touxiang"] forState:UIControlStateNormal];
        [_iconBtn setImage:[UIImage imageNamed:@"me_touxiang"] forState:UIControlStateNormal];
    }
    
    return _iconBtn;
}

#pragma mark - 入口
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = YSBJColorHui;
    [self.navigationController setNavigationBarHidden:NO];
    
//    [self.bottomTableView registerNib:[UINib nibWithNibName:@"MeXinxCell" bundle:nil] forCellReuseIdentifier:bjCellID];
    
    [self setupTouXiangView];
    
    [self addIconBtn];

}

//添加头像view
- (void)setupTouXiangView {
    UIView *touXiangView = [[UIView alloc] init];
    self.iconScrollView = touXiangView;
    touXiangView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:touXiangView];
    //添加约束
    [touXiangView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(64);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.3);
    }];
    
    
    
    //添加下面tableview
    UITableView *BjTableView = [[UITableView alloc] init];
    BjTableView.scrollEnabled = NO;
    
    BjTableView.delegate = self;
    BjTableView.dataSource = self;
    self.bottomTableView = BjTableView;
    
    [self.view addSubview:BjTableView];
    
    [BjTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(touXiangView.mas_bottom).offset(10);
        make.height.mas_equalTo(40 * (self.xinxArr.count + 1));
    }];
}

//添加头像按钮
- (void)addIconBtn {
    // 遍历出scrollView上所有的按钮从scrollView上移除
    for (UIView *view in self.iconScrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    // 计算宽高
    CGFloat imgW = (self.view.bounds.size.width - MARGIN * (COLCOUNT + 1)) / COLCOUNT;
    CGFloat imgH = imgW;
//    _btnWandH = imgH;
    
    for (int i = 0; i <= self.btnArr.count; i++) {
        // 计算行号和列号
        int rowNum = i / COLCOUNT;
        int colNum = i % COLCOUNT;
        
        // 计算x和y
        CGFloat imgX = MARGIN + (imgW + MARGIN) *colNum;
        CGFloat imgY = MARGIN + (imgH + MARGIN) *rowNum;
        
        // 创建按钮设置frame并添加到scrollView上
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //添加长按手势
        UILongPressGestureRecognizer *longPreGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressBtn)];
        [btn addGestureRecognizer:longPreGes];
        
        [btn setImage:[UIImage imageNamed:@"me_touxiang"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(imgX, imgY, imgW, imgH);
        btn.layer.cornerRadius = 5;
        [btn.layer setMasksToBounds:YES];
        [self.iconScrollView addSubview:btn];
        
        // 设置图片
        if (i < self.btnArr.count) {
//            UIImage *img = self.imagesData[i];
//            [btn setImage:img forState:UIControlStateNormal];
        } else {
//            //创建加号按钮
//            UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            // 设置加号按钮图片
            [btn setImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
            // 添加加号按钮点击事件
            [btn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
            // 设置scrollView滚动范围
//            self.iconScrollView.contentSize = CGSizeMake(self.view.bounds.size.width, CGRectGetMaxY(btn.frame) + MARGIN);
        }
    }
}

// 加号按钮点击事件
- (void)plusBtnClick {
    if (self.btnArr.count < 7) {
//        int rand = arc4random_uniform(7);
//        NSMutableArray *tjArr = [NSMutableArray array];
//        for (int i = 0; i < BTNCOUNT; i++) {
//            NSString *imgName = [NSString stringWithFormat:@"psb-%d", i+1];
//            UIImage *img = [UIImage imageNamed:imgName];
//            [tjArr addObject:img];
//        }
//    UIButton *btn = self.iconBtn;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.cornerRadius = 5;
//        UIImage *img = tjArr[rand];
        [self.btnArr addObject:btn];
        [self addIconBtn];
        
    }else {
        NSLog(@"--------------");
    }
}

//长按手势
- (void)longPressBtn {

}

//删除按钮
- (void)deleteBtn:(UIButton *)button {
    
}


#pragma mark - UITableViewDelegate&&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.xinxArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeXinxCell *cell = [tableView dequeueReusableCellWithIdentifier:bjCellID];
    if (cell == nil) {
//        cell = [[MeXinxCell alloc] init];
        cell = [[NSBundle mainBundle] loadNibNamed:@"MeXinxCell" owner:nil options:nil][0];
//        cell.name = self.xinxArr[indexPath.row];
//        [cell setCellWithName:self.xinxArr[indexPath.row]];
        [cell setCellWithName:self.xinxArr[indexPath.row] content:self.youArr[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",indexPath.row);
    if (indexPath.row == 0) {
        BianJiNameViewController *nameVC = [[BianJiNameViewController alloc] init];
        nameVC.title = @"编辑名字";
        nameVC.name = @"紫薇";
        
        [self.navigationController pushViewController:nameVC animated:YES];
    }else if (indexPath.row == 1) {
        YSQRCodeViewController *QRCodeVC = [[YSQRCodeViewController alloc] init];
//        QRCodeVC.title = @"我的二维码";
        QRCodeVC.ewmString = self.userName;
        [self.navigationController pushViewController:QRCodeVC animated:YES];
    }else if (indexPath.row == 2) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"性别" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *chooseNan = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *chooseNv = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:chooseNan];
        [alertVC addAction:chooseNv];
        
        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
}

@end
